package com.origin.app.business.immersive

import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.LinearLayoutManager
import com.origin.app.MyBaseLoadingMoreFooter
import com.origin.app.MyRefreshHeaderView
import com.origin.app.R
import com.origin.utils.softkeyboard.ImmersiveSoftKeyBoardUtil
import com.origin.utils.softkeyboard.SoftKeyBoardListener
import com.origin.app.databinding.LayoutBuImmerBinding
import com.origin.baselibrary.activity.style.ImmersiveActivity
import com.origin.uilibrary.xrecycleview.RecycleItemTouchHelper
import com.origin.uilibrary.xrecycleview.xr.XRecyclerView
import com.origin.utils.statusBar.StatusBarUtil
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import java.util.*

class BuImmersiveActivity : ImmersiveActivity<LayoutBuImmerBinding>(R.layout.layout_bu_immer) {
    var list = mutableListOf<String>()
    val teAdapter: TeAdapter by lazy {
        TeAdapter(list, mContext)
    }

    override fun pageLogical() {
        ImmersiveSoftKeyBoardUtil.assistActivity(this);
    }

    var isStatusBarLight = false
    fun changeStatusBar(view: View) {
        val mode = if (isStatusBarLight) {
            StatusBarUtil.statusBarLightMode(this)
        } else {
            StatusBarUtil.statusBarDarkMode(this)
        }
        isStatusBarLight = !isStatusBarLight

        Log.e("changeStatusBar", "mode ${mode} isStatusBarLight ${isStatusBarLight}")
    }

    override fun initContentView() {

        for (index in 1..100) {
            list.add("第 $index 项")
        }
        contentViewBinding.teRe.setRefreshHeader(MyRefreshHeaderView(this));
        contentViewBinding.teRe.setLoadingMoreFooter(MyBaseLoadingMoreFooter(this));
        contentViewBinding.teRe.setLoadingMoreEnabled(true)
        contentViewBinding.teRe.layoutManager = LinearLayoutManager(this)
        contentViewBinding.teRe.adapter = teAdapter
        val mCallBack =
            RecycleItemTouchHelper(object : RecycleItemTouchHelper.ItemTouchHelperCallback {
                override fun onItemDelete(positon: Int) {
                    list.removeAt(positon)
                    teAdapter.notifyItemRemoved(positon)
                }

                override fun onMove(fromPosition: Int, toPosition: Int) {
                    Collections.swap(list, fromPosition, toPosition);//交换数据
                    teAdapter.notifyItemMoved(fromPosition, toPosition);
                }

            })
//        contentViewBinding.teRe.setRefreshProgressStyle(ProgressStyle.BallBeat);
//        contentViewBinding.teRe.setLoadingMoreProgressStyle(ProgressStyle.BallRotate);
//
//        contentViewBinding.teRe
//            .defaultRefreshHeaderView
//            .setRefreshTimeVisible(true);
        val header = LayoutInflater.from(this).inflate(
            R.layout.recyclerview_header,
            findViewById<ViewGroup>(android.R.id.content), false
        );
        contentViewBinding.teRe.setLoadingListener(object : XRecyclerView.LoadingListener {
            override fun onRefresh() {
                GlobalScope.launch(context = Dispatchers.Main) {
                    delay(3000)
                    contentViewBinding.teRe.refreshComplete()
                }
            }

            override fun onLoadMore() {
                GlobalScope.launch(context = Dispatchers.Main) {
                    delay(3000)
                    list.add("第 a 项")
                    list.add("第 b 项")
                    list.add("第 c 项")
                    list.add("第 d 项")
                    teAdapter.notifyDataSetChanged()
                    contentViewBinding.teRe.loadMoreComplete()
                }
            }

        })
//        contentViewBinding.teRe.addHeaderView(header);
        SoftKeyBoardListener.setListener(this, object :
            SoftKeyBoardListener.OnSoftKeyBoardChangeListener {
            override fun keyBoardShow(height: Int) {
                contentViewBinding.teRe.scrollToPosition(teAdapter.itemCount - 1);
            }

            override fun keyBoardHide(height: Int) {
            }

        })
    }
}
